#pragma once

#include "Callback.h"

using namespace AT;

class CDelayedTask
{
public:
	CDelayedTask();
	~CDelayedTask();

	bool Init();
	void Exit();
	bool PostTask(UINT nDelayMs, ICallbackPtr&& pTask);

private:
	void RunTask();

	struct TimedTask
	{
		union
		{
			FILETIME ftDueTime;
			INT64    nDueTime;
		};

		ICallbackPtr pTask;

		bool operator<(const TimedTask& task) const
		{
			if (nDueTime != task.nDueTime)
				return nDueTime > task.nDueTime;
			else
				return pTask->GetId() > task.pTask->GetId();
		}
	};

	PTP_TIMER m_pTpTimer;
	SRWLOCK   m_SRWLock = SRWLOCK_INIT;
	std::priority_queue<TimedTask> m_queTasks;
};
